var express = require('express');
var router = express.Router();
var logger = require('../common/logger').logger.getLogger("user");
var mysql_query = require('../common/mysql');
var httpresult = require('../common/response');
var chapter_model = require('../models/chapter');
// router.post('/:course_id/list', async function(req, res, next) {
//     var [error, data] = await mysql_query("select chapter_id, course_id, name, introduce from chapter where course_id = ? and chapter_id in (?) ", [req.params.course_id, req.body]);
//     if (error) {
//         logger.warn(error);
//         return res.send(httpresult.sys_error(error));
//     }
//     if (data.length === 0) {
//         return res.send(httpresult.trans_error("COURSE_NOT_EXIST", "该章节不存在"));
//     }
//     var result = data;
//     return res.send(httpresult.success(result));
// });

router.post('/list', async function(req, res, next) {
    let [error, data] = await chapter_model.list({course_id: req.body.course_id, condition: req.body.condition});
    if (error) {
        logger.warn(error);
        return res.send(httpresult.sys_error(error));
    }
    return res.send(httpresult.success(data));
});

router.post('/add', async function(req, res, next) {
    let new_order_number = req.body.order_number;
    if (req.body.order_number === "") {
        var [error, data] = await mysql_query(
            "select count(*) from chapter where course_id = ? and parent_id = ?",
            [req.body.course_id, req.body.parent_id]);
        if (error) {
            logger.warn(error);
            return res.send(httpresult.sys_error(error));
        }
        new_order_number = data[0]['count(*)'] + 1;
    }
    else {
        var [error, data] = await mysql_query(
            "update chapter set order_number = order_number + 1 where course_id = ? and parent_id = ? and order_number >= ? ",
            [req.body.course_id, req.body.parent_id, req.body.order_number])
        if (error) {
            logger.warn(error);
            return res.send(httpresult.sys_error(error));
        }
    }
    var [error, data] = await mysql_query(
        "insert into chapter (course_id, name, introduce, order_number, parent_id) value (?, ?, ?, ?, ?)",
        [req.body.course_id, req.body.name, req.body.introduce, new_order_number, req.body.parent_id]);
    if (error) {
        logger.warn(error);
        return res.send(httpresult.sys_error(error));
    }
    var result = data;
    return res.send(httpresult.success(result));
});

router.post('/update', async function(req, res, next) {
    var [error, data] = await mysql_query("update chapter set course_id = ?, name = ?, introduce = ? where chapter_id = ?",
        [req.body.course_id, req.body.name, req.body.introduce, req.body.chapter_id]);
    if (error) {
        logger.warn(error);
        return res.send(httpresult.sys_error(error));
    }
    var result = data;
    return res.send(httpresult.success(result));
});

router.post('/delete', async function(req, res, next) {
    var [error, data] = await mysql_query("select parent_id from chapter where chapter_id = ?", [req.body.chapter_id]);
    if (error) {
        logger.warn(error);
        return res.send(httpresult.sys_error(error));
    }
    if (data.length === 0) {
        return res.send(httpresult.trans_error("CHAPTER_NOT_EXIST", "该章节不存在"));
    }
    var parent_id = data[0].parent_id;

    var [error, data] = await mysql_query("delete from chapter where chapter_id = ?", [req.body.chapter_id]);
    if (error) {
        logger.warn(error);
        return res.send(httpresult.sys_error(error));
    }
    [error, data] = await mysql_query(
        "update chapter set order_number = order_number - 1 where course_id = ? and parent_id = ? and order_number > ? ",
        [req.body.course_id, parent_id, req.body.order_number]);
    if (error) {
        logger.warn(error);
        return res.send(httpresult.sys_error(error));
    }
    var result = data;
    return res.send(httpresult.success(result));
});

router.post('/sort', async function(req, res, next) {
    var sql = "update chapter set order_number = case chapter_id ";
    var where_params = [];
    var in_ids = [];
    for (let chapter_id in req.body.data) {
        sql += " when ? then ? "
        where_params.push(Number(chapter_id));
        where_params.push(req.body.data[chapter_id]);
        in_ids.push(Number(chapter_id));
    }
    sql += " end where chapter_id in (?)";
    where_params.push(in_ids);
    var [error, data] = await mysql_query(sql, where_params);
    if (error) {
        logger.warn(error);
        return res.send(httpresult.sys_error(error));
    }
    var result = data;
    return res.send(httpresult.success(result));
});

module.exports = router;
